<?php
namespace app\model;
use app\BaseModel;
use think\model\concern\SoftDelete;
use app\model\Company;
use app\model\WorkerType;
use app\model\Depart;


class WorkerRecords extends BaseModel
{

    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = '0000-00-00 00:00:00';
    protected $hidden = ['update_time','delete_time','sort','status'];


    public function searchCompanyIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('company_id', '=', $value);
        }
    }

    public function searchWorkerIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('worker_id', '=', $value);
        }
    }

    public function searchWorkerDepartIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('worker_depart_id', '=', $value);
        }
    }

    public function searchWorkerTypeIdAttr($query, $value, $data)
    {
        if (!empty($value)) {
            $query->where('worker_type_id', '=', $value);
        }
    }


    protected function setCompanyIdAttr($value, $data)
    { //当前字段的修改器
        $this->set('company_title', Company::where('id', $value)->value('title'));
        return $value;
    }

    protected function setWorkerIdAttr($value, $data)
    { //当前字段的修改器
        $find = Worker::where('id', $value)->find();
        $this->set('worker_name', $find['name']);
        $this->set('worker_mobile', $find['mobile']);
        $this->set('worker_depart_id', $find['depart_id']);
        $this->set('worker_depart_title', $find['depart_title']);
        $this->set('worker_type_id', $find['worker_type_id']);
        $this->set('worker_type_title', $find['worker_type_title']);


        return $value;
    }

    public static function onAfterInsert($data){
        $find = Worker::where('id',$data['worker_id'])->find();

        Worker::where('id', $find['id'])->inc('end_number', $data['price'])->update();
        WorkerType::where('id', $find['worker_type_id'])->inc('end_number', $data['price'])->update();
        Depart::where('id', $find['depart_id'])->inc('end_number', $data['price'])->update();
        Company::where('id', $find['company_id'])->inc('end_number', $data['price'])->update();

    }
//    protected function setWorkerDepartIdAttr($value,$data){ //当前字段的修改器
//         $this->set('worker_depart_title',Depart::where('id',$value)->value('title'));
//         return $value;
//    }
//    protected function setWorkerDepartTypeIdAttr($value,$data){ //当前字段的修改器
//         $this->set('worker_depart_type_title',WorkerType::where('id',$value)->value('title'));
//         return $value;
//    }
//




}